/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package web;

import ejb.entity.Client;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Queue;
import javax.jms.Session;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Trasheur
 */
@WebServlet(name = "AjouterClient", urlPatterns = {"/AjouterClient"})
public class AjouterClient extends HttpServlet {

    @Resource(mappedName = "jms/NewClientMessageFactory")
    private ConnectionFactory connectionFactory;
    @Resource(mappedName = "jms/NewClientMessage")
    private Queue queue;

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");

        // Add the following code to send the JMS message
        String nom = request.getParameter("nom");
        String prenom = request.getParameter("prenom");
        String date_de_naissance = request.getParameter("date_de_naissance");
        String adresse = request.getParameter("adresse");
        if ((nom != null) && (prenom != null) && (date_de_naissance != null) && (adresse != null)) {
            try {
                Connection connection = connectionFactory.createConnection();
                Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
                MessageProducer messageProducer = session.createProducer(queue);

                ObjectMessage message = session.createObjectMessage();

                Client e = new Client();
                e.setNom(nom);
                e.setPrenom(prenom);
                try {
                    e.setDateDeNaissance(new SimpleDateFormat("yyyy-MM-dd").parse(date_de_naissance));
                } catch (ParseException ex) {
                    Logger.getLogger(AjouterClient.class.getName()).log(Level.SEVERE, null, ex);
                }
                e.setAdresse(adresse);
                e.setDateDeCreation(new Date());
                e.setNombreCompte(0);

                message.setObject(e);
                messageProducer.send(message);
                messageProducer.close();
                connection.close();
                response.sendRedirect("ListClient");
            } catch (JMSException ex) {
                ex.printStackTrace();
            }
        }

        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here. You may use following sample code. */
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet AjouterClient</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet AjouterClient at " + request.getContextPath() + "</h1>");
            // The following code adds the form to the web page
            out.println("<form>");
            out.println("Nom : <input type='text' name='nom'><br/>");
            out.println("Prenom : <input type='text' name='prenom'><br/>");
            out.println("Date de naissance : <input type='date' name='date_de_naissance''><br/>");
            out.println("Adresse : <input type='text' name='adresse'><br/>");
            out.println("<input type='submit'><br/>");
            out.println("</form>");
            out.println("</body>");
            out.println("</html>");
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
